package com.cqfae.deploycentral.app.web.controller;

import com.cqfae.deploycentral.app.utils.GsonUtils;
import com.cqfae.deploycentral.app.base.BaseConstants;
import com.cqfae.deploycentral.app.services.CommonControllerService;
import com.cqfae.deploycentral.app.services.InspectService;
import com.cqfae.deploycentral.app.utils.NullUtil;
import com.cqfae.deploycentral.app.web.request.AppInfoRequest;
import com.cqfae.deploycentral.app.web.request.InspectRequest;
import com.cqfae.deploycentral.app.web.response.WebResponse;
import com.cqfae.deploycentral.app.web.vo.InspectAreaAppVo;
import com.cqfae.deploycentral.app.web.vo.InspectHistoryVo;
import com.cqfae.deploycentral.app.web.vo.InspectStatisticsVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 巡查相关操作
 *
 * @author JIANGLU009
 * @data 2017-12-21
 */
@RestController
@RequestMapping("/deploy-central/inspect")
public class InspectController implements CommonControllerService<Object> {

    private static Logger logger = LoggerFactory.getLogger(InspectController.class);

    @Autowired
    private InspectService inspectService;

    /**
     * 查询城市
     *
     * @return
     */
    @RequestMapping(value = "/get-city", method = RequestMethod.POST)
    public WebResponse getCity() {
        logger.info("==== path:/inspect/get-city getCity:==== ");
        return getWebResponse(inspectService.getCity());
    }

    /**
     * 查询巡查统计
     *
     * @param inspectStatisticsVo
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    public WebResponse getStatistics(InspectStatisticsVo inspectStatisticsVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectStatisticsVo);
        logger.info("==== path:/inspect/statistics InspectStatisticsVo:{}==== ", logJson);
        return getWebResponse(inspectService.getStatistics(inspectStatisticsVo));
    }

    /**
     * 查询巡查历史表
     *
     * @param inspectRequest
     * @return
     */
    @RequestMapping(value = "/history", method = RequestMethod.GET)
    public WebResponse getHistory(InspectRequest inspectRequest) {
        String logJson = GsonUtils.getInstance().toJson(inspectRequest);
        logger.info("==== path:/inspect/history getHistory:{}==== ", logJson);
        if ((inspectRequest.getBeginDate() > 0 || inspectRequest.getEndDate() > 0)
                && inspectRequest.getBeginDate() > inspectRequest.getEndDate()) {
            return getWebErrorResponse("日期不合法，请检查开始结束日期");
        }
        return getWebResponse(inspectService.getHistory(inspectRequest));
    }

    /**
     * 查询巡查历史详情表
     *
     * @param inspectRequest
     * @return
     */
    @RequestMapping(value = "/history/item", method = RequestMethod.GET)
    public WebResponse getHistoryItem(InspectRequest inspectRequest) {
        String logJson = GsonUtils.getInstance().toJson(inspectRequest);
        logger.info("==== path:/inspect/history/item getHistoryItem:{}==== ", logJson);
        if (inspectRequest.getHistoryId() == null) {
            return getWebErrorResponse("巡检历史ID为空");
        }
        return getWebResponse(inspectService.getHistoryItem(inspectRequest));
    }

    /**
     * 健康巡查
     *
     * @param inspectHistoryVo
     * @return
     */
    @RequestMapping(value = "/health-check", method = RequestMethod.GET)
    public WebResponse healthCheck(InspectHistoryVo inspectHistoryVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectHistoryVo);
        logger.info("==== path:/inspect/health-check healthCheck:{}==== ", logJson);
        if (StringUtils.isBlank(inspectHistoryVo.getAppName())) {
            return getWebErrorResponse("健康巡查应用名为空");
        }
        return getWebResponse(inspectService.healthCheck(inspectHistoryVo));
    }

    /**
     * 健康巡查jenkins回调
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "/health-check/call-back")
    public WebResponse healthCheckCallBack(@RequestParam String json) {
        logger.info("==== path:/inspect/health-check/call-back healthCheckCallBack:{}==== ", json);
        return getWebResponse(inspectService.healthCheckCallBack(json));
    }

    /**
     * 区域城市配置新增
     *
     * @param inspectAreaAppVo
     * @return
     */
    @RequestMapping(value = "/area-app/add", method = RequestMethod.POST)
    public WebResponse areaAppAdd(@RequestBody InspectAreaAppVo inspectAreaAppVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectAreaAppVo);
        logger.info("==== path:/inspect/area-app/add areaAppAdd:{}==== ", logJson);
        return getWebResponse(inspectService.areaAppAdd(inspectAreaAppVo));
    }

    /**
     * 区域城市配置编辑
     *
     * @param inspectAreaAppVo
     * @return
     */
    @RequestMapping(value = "/area-app/edit", method = RequestMethod.POST)
    public WebResponse areaAppEdit(@RequestBody InspectAreaAppVo inspectAreaAppVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectAreaAppVo);
        logger.info("==== path:/inspect/area-app/edit areaAppEdit:{}==== ", logJson);
        if (inspectAreaAppVo.getId() == null) {
            return getWebErrorResponse("ID为空");
        }
        return getWebResponse(inspectService.areaAppEdit(inspectAreaAppVo));
    }

    /**
     * 区域城市配置删除
     *
     * @param inspectAreaAppVo
     * @return
     */
    @RequestMapping(value = "/area-app/delete", method = RequestMethod.POST)
    public WebResponse areaAppDelete(@RequestBody InspectAreaAppVo inspectAreaAppVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectAreaAppVo);
        logger.info("==== path:/inspect/area-app/delete areaAppDelete:{}==== ", logJson);
        if (inspectAreaAppVo.getId() == null) {
            return getWebErrorResponse("ID为空");
        }
        return getWebResponse(inspectService.areaAppDelete(inspectAreaAppVo));
    }

    /**
     * 区域城市配置翻页查询
     *
     * @param inspectRequest
     * @return
     */
    @RequestMapping(value = "/area-app/list-by-page", method = RequestMethod.GET)
    public WebResponse areaAppListByPage(InspectRequest inspectRequest) {
        String logJson = GsonUtils.getInstance().toJson(inspectRequest);
        logger.info("==== path:/inspect/area-app/list-by-page areaAppListByPage:{}==== ", logJson);
        return getWebResponse(inspectService.areaAppListByPage(inspectRequest));
    }

    /**
     * 区域城市配置单个查询
     *
     * @param inspectAreaAppVo
     * @return
     */
    @RequestMapping(value = "/area-app/get-one", method = RequestMethod.GET)
    public WebResponse areaAppGetOne(InspectAreaAppVo inspectAreaAppVo) {
        String logJson = GsonUtils.getInstance().toJson(inspectAreaAppVo);
        logger.info("==== path:/inspect/area-app/get-one areaAppGetOne:{}==== ", logJson);
        if (inspectAreaAppVo.getId() == null) {
            return getWebErrorResponse("ID为空");
        }
        return getWebResponse(inspectService.areaAppGetOne(inspectAreaAppVo));
    }

    /**
     * 获得所有有效的app
     *
     * @return
     */
    @RequestMapping(value = "/area-app/get-app-list")
    public WebResponse getAppList(AppInfoRequest request) {
        String logJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== path:/inspect/area-app/get-app-list getAppList:{}==== ", logJson);
        return getWebResponse(inspectService.getAppList(request));
    }

    /**
     * 健康巡查后门
     *
     * @return
     */
    @RequestMapping(value = "/health-check/call-hand")
    public WebResponse healthCheckCallHand(InspectHistoryVo inspectHistoryVo) {
        logger.info("==== path:/inspect/health-check/call-hand :{}==== ", inspectHistoryVo);
        if (StringUtils.isBlank(inspectHistoryVo.getAppName())) {
            return getWebResponse(inspectService.execJenkinsInspect());
        } else {
            return getWebResponse(inspectService.healthCheck(inspectHistoryVo));
        }
    }

    /**
     * 设置返回参数
     *
     * @param obj
     * @return
     */
    @Override
    public WebResponse getWebResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        if (NullUtil.isNotEmpty(obj)) {
            webResponse.setData(obj);
        }
        return webResponse;
    }

    /**
     * 设置异常情况下返回参数
     *
     * @param obj
     * @return
     */
    public WebResponse getWebErrorResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        webResponse.setResultCode(BaseConstants.WEB_ERROR_CODE);
        if (NullUtil.isNotEmpty(obj)) {
            webResponse.setData(obj);
        }
        return webResponse;
    }


}
